package com.fanyu.test.personal;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.UUID;

import org.springframework.util.DigestUtils;

import com.alibaba.fastjson.JSONObject;
import com.fanyu.framework.utils.HttpClients4;
import com.fanyu.framework.utils.UrlUtil;

/**
 * 接入检测系统（传递文件直接到支付页面）
 */

public class AutoLogin {
    public static void main(String[] args) {
        // 请求参数
        String key = "***"; // 由维普提供的密匙
        String timeStamp = new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date()); // 时间戳精确到毫秒，将验证时效性
        String customparameter = String.valueOf(UUID.randomUUID()).replaceAll("-", ""); // 这里采用GUID作为参考，要具有唯一性
        String url = "***"; // 下载单个docx、doc、pdf、txt文件的地址
        String author = "***"; // 作者
        String username = "***"; // 用户名不超过16位,为学生学号，维普需要根据学号处理红包需求
        String title = "***"; // 标题
        String userid = "***"; // 由维普提供的userid

        // 组装请求参数
        LinkedHashMap<String, String> param = new LinkedHashMap<>();
        param.put("author", author);
        param.put("username", username);
        param.put("title", title);
        param.put("timestamp", timeStamp);
        param.put("url", url);
        param.put("customparameter", customparameter);
        param.put("userid", userid);
        try {
            String strParams = "author=" + author + "&username=" + username + "&title=" + title + "&timestamp=" + timeStamp + "&url=" + URLEncoder.encode(url, "UTF-8");
            strParams = strParams + "&key=" + key;
            String sign = DigestUtils.md5DigestAsHex(strParams.getBytes(StandardCharsets.UTF_8)).toUpperCase(); // 组装sign并进行MD5加密
            param.put("sign", sign);

            // 推送参数,可拿到浏览器去测试
            String pushUrl = "https://vpcsapi.fanyu.com/personal/Library/AutoLogin.aspx" + "?" + UrlUtil.asUrlParams(param);

            // 模拟浏览器发送Get请求
            HttpClients4 clients4 = new HttpClients4();
            HttpClients4.HttpClientResult hcr = clients4.get(pushUrl);

            // 查看返回内容
            JSONObject jsonObject = JSONObject.parseObject(hcr.getBodyAsString());
            System.out.println(jsonObject);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
